package com.demo.pdf.controller;

import com.demo.pdf.service.PdfFillService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.Map;

@RestController
@RequestMapping("/api/pdf")
public class PdfController {

    @Autowired
    private PdfFillService pdfFillService;

    /**
     * 填充PDF表单并返回结果
     *
     * @param templateFile PDF模板文件
     * @param fieldValues  字段值映射（JSON格式）
     * @return 填充后的PDF文件
     */
    @PostMapping("/fill-form")
    public ResponseEntity<byte[]> fillPdfForm(
            @RequestParam("template") MultipartFile templateFile,
            @RequestBody Map<String, String> fieldValues) {
        
        try {
            // 保存上传的模板文件到临时位置
            File tempFile = File.createTempFile("pdf-template-", ".pdf");
            templateFile.transferTo(tempFile);

            // 填充PDF表单
            byte[] filledPdf = pdfFillService.fillPdfForm(tempFile.getAbsolutePath(), fieldValues);

            // 删除临时文件
            tempFile.delete();

            // 设置响应头
            HttpHeaders headers = new HttpHeaders();
            headers.setContentType(MediaType.APPLICATION_PDF);
            headers.setContentDispositionFormData("attachment", "filled-form.pdf");

            return new ResponseEntity<>(filledPdf, headers, HttpStatus.OK);
        } catch (IOException e) {
            return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
        }
    }

    /**
     * 创建简单的文本PDF
     *
     * @param content 文本内容
     * @return PDF文件
     */
    @PostMapping("/create-text")
    public ResponseEntity<byte[]> createTextPdf(@RequestBody Map<String, String> content) {
        try {
            String text = content.getOrDefault("text", "");
            byte[] pdfBytes = pdfFillService.createSimpleTextPdf(text);

            HttpHeaders headers = new HttpHeaders();
            headers.setContentType(MediaType.APPLICATION_PDF);
            headers.setContentDispositionFormData("attachment", "text-document.pdf");

            return new ResponseEntity<>(pdfBytes, headers, HttpStatus.OK);
        } catch (IOException e) {
            return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
        }
    }

    /**
     * 在现有PDF上添加文本
     *
     * @param templateFile PDF模板文件
     * @param text         要添加的文本
     * @param x            X坐标
     * @param y            Y坐标
     * @return 添加文本后的PDF文件
     */
    @PostMapping("/add-text")
    public ResponseEntity<byte[]> addTextToPdf(
            @RequestParam("template") MultipartFile templateFile,
            @RequestParam("text") String text,
            @RequestParam("x") float x,
            @RequestParam("y") float y) {
        
        try {
            // 保存上传的模板文件到临时位置
            File tempFile = File.createTempFile("pdf-template-", ".pdf");
            templateFile.transferTo(tempFile);

            // 添加文本到PDF
            byte[] modifiedPdf = pdfFillService.addTextToPdf(tempFile.getAbsolutePath(), text, x, y);

            // 删除临时文件
            tempFile.delete();

            // 设置响应头
            HttpHeaders headers = new HttpHeaders();
            headers.setContentType(MediaType.APPLICATION_PDF);
            headers.setContentDispositionFormData("attachment", "modified-pdf.pdf");

            return new ResponseEntity<>(modifiedPdf, headers, HttpStatus.OK);
        } catch (IOException e) {
            return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
        }
    }
}